package com.dzl.admin;

import com.alibaba.fastjson.JSONObject;
import com.dzl.admin.service.SysAdminService;
import com.dzl.base.controller.BaseController;
import com.dzl.constant.CommonConstants;
import com.dzl.supplier.pojo.WelfareSupplier;
import com.dzl.supplier.pojo.vo.WelfareSupplierLoginVo;
import com.dzl.supplier.service.WelfareSupplierService;
import com.dzl.util.EmptyUtils;
import com.dzl.util.JsonResult;
import com.dzl.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Saas服务端用户表(SysAdmin)表控制层
 * @author caobucao
 * @since 2021-05-13 15:35:06
 */
@Slf4j
@RestController
@RequestMapping("sysAdmin")
public class SysAdminController extends BaseController {

    @Autowired
    private SysAdminService sysAdminService;
    @Autowired
    private WelfareSupplierService welfareSupplierService;

    /**
     * 供应商端登录验证
     * @date 2021/10/11 17:07
     * @author caobucao
     * @param query name:用户名 password：密码
     * @return com.dzl.util.JsonResult<java.lang.String>
     */
    @PostMapping(value="/login")
    public JsonResult<String> login(HttpServletRequest request, @RequestBody JSONObject query) {
        String name=query.getString("name");
        String password=query.getString("password");
        if(EmptyUtils.isEmpty(name)||EmptyUtils.isEmpty(password)) {
            return JsonResult.paramErrWithMsg("请输入用户名或密码");
        }
        //WelfareSupplier welfareSupplier = welfareSupplierService.selectWelfareSupplierByName(name);
        WelfareSupplier welfareSupplierQuery = new WelfareSupplier();
        welfareSupplierQuery.setName(name);
        welfareSupplierQuery.setStatus(CommonConstants.YES);
        List<WelfareSupplier> list = welfareSupplierService.select(welfareSupplierQuery);
        WelfareSupplier welfareSupplier = EmptyUtils.isNotEmpty(list)?list.get(0):null;
        if(EmptyUtils.isEmpty(welfareSupplier)){
            return JsonResult.paramErrWithMsg("该供应商未注册");
        }
        if(welfareSupplier.getIsOpen()!=CommonConstants.YES){
            return JsonResult.paramErrWithMsg("该供应商未开通");
        }
        String token=sysAdminService.supplierLoginVerify(welfareSupplier, name, password);
        return JsonResult.success(token);
    }

    /**
     * 退出登录
     * @date 2021/5/14 17:15
     * @author caobucao
     * @param request
     * @return com.dzl.util.JsonResult<java.lang.Integer>
     */
    @GetMapping("/logout")
    public JsonResult<Integer> logout(HttpServletRequest request) {
        String token=request.getHeader("token");
        if(EmptyUtils.isEmpty(token)) {
            return JsonResult.fail();
        }
        RedisUtil.delete(token);
        return JsonResult.success();
    }

    /**
     * 获取登录供应商的详细信息
     * @date 2021/10/11 17:26
     * @author caobucao
     * @return com.dzl.util.JsonResult<com.dzl.supplier.pojo.vo.WelfareSupplierLoginVo>
     */
    @GetMapping("/getLoginSupplierInfo")
    public JsonResult<WelfareSupplierLoginVo> getLoginSupplierInfo_() {
        return JsonResult.success(getLoginSupplierInfo());
    }

}
